import { ResPage, ReqPage } from "@/api/interface/index";
import { PORT1 } from "@/api/config/servicePort";
import http from "@/api";

export namespace MBOM {
  export interface MBOMListItem {
    id: number;
    mbomCode: string;
    mbomName: string;
    productId: number;
    productName: string;
    version: string;
    status: string;
    description: string;
    ebomId?: number;
    ebomVersion?: string;
    reviewerId?: number;
    reviewerName?: string;
    submitTime?: string;
    reviewTime?: string;
    releaseTime?: string;
    obsoleteTime?: string;
    createdBy: string;
    createdTime: string;
    updatedBy: string;
    updatedTime: string;
  }

  export interface CreateMBOMRequest {
    mbomCode: string;
    mbomName: string;
    productId: number;
    version: string;
    description?: string;
  }

  export interface UpdateMBOMRequest {
    mbomName: string;
    description?: string;
  }

  export interface SyncMBOMFromEBOMRequest {
    ebomId: number;
    ebomVersion: string;
    productId: number;
  }

  export interface SubmitMBOMReviewRequest {
    reviewerId: number;
    submitComment?: string;
  }

  export interface ApproveMBOMRequest {
    approveComment?: string;
  }

  export interface RejectMBOMRequest {
    rejectReason: string;
  }

  export interface AssignMBOMToWorkOrderRequest {
    workOrderId: number;
  }

  export interface ObsoleteMBOMRequest {
    obsoleteReason: string;
  }
}

/**
 * 获取MBOM列表
 */
export const getMBOMList = (params: ReqPage & Partial<MBOM.MBOMListItem>) => {
  return http.get<ResPage<MBOM.MBOMListItem>>(`/mbom/list`, params);
};

/**
 * 创建MBOM
 */
export const createMBOM = (params: MBOM.CreateMBOMRequest) => {
  return http.post<number>(`/mbom/create`, params);
};

/**
 * 从EBOM同步创建MBOM
 */
export const syncMBOMFromEBOM = (params: MBOM.SyncMBOMFromEBOMRequest) => {
  return http.post<number>(`/mbom/sync-from-ebom`, params);
};

/**
 * 更新MBOM
 */
export const updateMBOM = (id: number, params: MBOM.UpdateMBOMRequest) => {
  return http.put(`/mbom/${id}`, params);
};

/**
 * 获取MBOM详情
 */
export const getMBOMById = (id: number) => {
  return http.get<MBOM.MBOMListItem>(`/mbom/${id}`);
};

/**
 * 提交审核
 */
export const submitMBOMForReview = (id: number, params: MBOM.SubmitMBOMReviewRequest) => {
  return http.put(`/mbom/${id}/submit-review`, params);
};

/**
 * 审核通过
 */
export const approveMBOM = (id: number, params: MBOM.ApproveMBOMRequest) => {
  return http.put(`/mbom/${id}/approve`, params);
};

/**
 * 审核拒绝
 */
export const rejectMBOM = (id: number, params: MBOM.RejectMBOMRequest) => {
  return http.put(`/mbom/${id}/reject`, params);
};

/**
 * 发布MBOM
 */
export const releaseMBOM = (id: number) => {
  return http.put(`/mbom/${id}/release`);
};

/**
 * 分配给工单
 */
export const assignMBOMToWorkOrder = (id: number, params: MBOM.AssignMBOMToWorkOrderRequest) => {
  return http.put(`/mbom/${id}/assign-workorder`, params);
};

/**
 * 作废MBOM
 */
export const obsoleteMBOM = (id: number, params: MBOM.ObsoleteMBOMRequest) => {
  return http.put(`/mbom/${id}/obsolete`, params);
};

/**
 * 根据产品获取有效MBOM列表
 */
export const getActiveMBOMByProductId = (productId: number) => {
  return http.get<MBOM.MBOMListItem[]>(`/mbom/product/${productId}/active`);
};

/**
 * 根据状态获取MBOM列表
 */
export const getMBOMByStatus = (status: string) => {
  return http.get<MBOM.MBOMListItem[]>(`/mbom/status/${status}`);
};
